﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using TimeSplicerModule.Controllers;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Infrastucture;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for StatusListView.xaml
    /// </summary>
    public partial class StatusListView : UserControl, INavigationAware
    {
        private StatusListViewModel Model;
        private readonly IUnityContainer _container;
        private readonly IStatusFactory _statusFactory;
        private readonly IStatusController _statusController;

        public StatusListView(IUnityContainer container, IStatusFactory statusFactory,
            IStatusController statusController)
        {
            InitializeComponent();

            _container = container;
            _statusFactory = statusFactory;
            _statusController = statusController;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
            Model = new StatusListViewModel();
            Model.Statuses = new EditableList<StatusModel>();

            var statuses = _statusFactory.GetList();
            
            foreach (var s in statuses)
            {
                Model.Statuses.Add(s);
            }

            Model.Statuses.ItemEndEdit += new DomainObject.ItemEndEditEventHandler(statusEditItem);
            Model.Statuses.CollectionChanged += Statuses_CollectionChanged;

            //var test = _statusFactory.Get(x => x.Name == "Testing"
            //    && x.IsActive != false
            //    || x.IsTaskStatus);

            this.DataContext = Model;
        }

        void Statuses_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // not sure if i need to do something with this
            //throw new NotImplementedException();
        }

        // need to do something about saving

        private void statusEditItem(IEditableObject sender)
        {
            var status = sender as StatusModel;
            status = _statusFactory.Upsert(status);
        }
    }
}
